//
//  RiskFilter.js
//  SugLisSystem
//  险种列表过滤选择器
//  Created by Mr.Right on 2012-12-02.
//  Copyright 2012 Mr.Right. All rights reserved.
//
//增强Array
Ti.include('prototypeextr/PrototypeExtr_Array.js');
var dB = require('untils/DB');
var pubFun = require('untils/PubFun');
var exeSQL = require('untils/ExeSQL');

/**
 * 主险筛选
 * @param tGlobalInput 全局变量
 * @param riskType 寿险类型
 * @param contNo 建议书号
 * @param insuredNo 被保人号
 * @return 可投保的主险
 * */
function getRiskListForType(tGlobalInput, riskType, contNo, insuredNo) {
	Ti.API.warn('[RiskFilter]-------------筛选主险开始--------------');
	// 1、险种互斥
	var set_1 = getMainRiskForTypeExclude(contNo, insuredNo);
	Ti.API.warn('[RiskFilter]险种互斥:' + set_1.length + '个');
	// 2、险种渠道，地区，年龄
	// Set set_2 = getRiskForChnlAreaAge(tGlobalInput, contNo, insuredNo);
	var set_2 = getSaleRiskAndPlan(tGlobalInput, contNo, insuredNo);
	Ti.API.warn('[RiskFilter]地区年龄渠道:' + set_2.length + '个');
	// 3、险种类别
	var set_3 = getRiskForRiskType(riskType);
	Ti.API.warn('[RiskFilter]险种类别:' + set_3.length + '个');
	// 4、险种销售组
	// var set_4 = getRiskForRiskSaleGroup(tGlobalInput);
	// 5、排除已经投保的
	var set_5 = getMainRiskForExist(contNo, insuredNo);
	Ti.API.warn('[RiskFilter]已经投保:' + set_5.length + '个');

	// 求交集
	var set = [];
	var riskList = [];
	for (var i = 0; i < set_2.length; i++) {
		var riskcode = set_2[i];
		//&& set_4 != null && set_4.contains(riskcode)
		if (set_1 != null && set_1.indexOf(riskcode) != -1 && set_3 != null && set_3.indexOf(riskcode) != -1 && set_5 != null && set_5.indexOf(riskcode) != -1) {
			set.push("'" + riskcode + "'");
		}
	}

	if (set.length > 0) {
		var sql = "select * from lmrisk where riskcode in (" + set + ") order by riskcode";
		var reLMRiskDB = new dB('LMRisk');
		riskList = reLMRiskDB.executeQuery(sql);
	}
	Ti.API.warn('[RiskFilter]-------------筛选主险开始--------------');
	return riskList;
}

exports.getRiskListForType = getRiskListForType;

/**
 * 附件险筛选
 * @param tGlobalInput 全局变量
 * @param mainRiskCode 主险代码
 * @param homeRiskFlag 家庭险标记 -1排除普通附加险，0排除家庭险
 * @param contNo 建议书号
 * @param insuredNo 被保人号
 * @return 可投保的附加险
 * */
function getSubRiskList(tGlobalInput, mainRiskCode, homeRiskFlag, contNo, insuredNo) {
	Ti.API.warn('[RiskFilter]-------------筛选附加险开始--------------');
	// 1、险种渠道，地区，年龄的筛选
	var set_1 = getSaleRiskAndPlan(tGlobalInput, contNo, insuredNo);
	Ti.API.warn('[RiskFilter]险种渠道，地区，年龄的筛选:' + set_1.length + '个');
	// 2、险种销售组
	//var set_2 = getRiskForRiskSaleGroup(tGlobalInput);
	// 3、主附关系渠道，区域，年龄的筛选
	//var set_3 = null;
	// 4、排除已经投保的
	//不排除任何投保的，有则展示
	// var set_4 = getSubRiskForCurrentCondition(tGlobalInput, contNo, insuredNo);
	// Ti.API.warn('[RiskFilter]排除已经投保的:' + set_4.length + '个');
	var set_5 = getSubRiskForMainRisk(mainRiskCode, homeRiskFlag);
	Ti.API.warn('[RiskFilter]家庭计划筛选:' + set_5.length + '个');
	var riskList = [];
	var set = [];
	var tempSet = [];
	// for (var i = 0; i < set_4.length; i++) {
	// tempSet.push(set_4[i]);
	// }

	for (var i = 0; i < set_5.length; i++) {
		tempSet.push(set_5[i]);
	}

	for (var i = 0; i < tempSet.length; i++) {
		var riskcode = tempSet[i];
		// && set_2 != null && set_2.contains(riskcode)
		if (set_1 != null && set_1.indexOf(riskcode) != -1) {
			set.push("'" + riskcode + "'");
		}
	}

	if (set.length > 0) {
		var sql = "select * from lmrisk where riskcode in (" + set + ") order by riskcode";
		var reLMRiskDB = new dB('LMRisk');
		riskList = reLMRiskDB.executeQuery(sql);
	}
	Ti.API.warn('[RiskFilter]-------------筛选附加险结束--------------');
	return riskList;
}

exports.getSubRiskList = getSubRiskList;

/**
 * 根据现有险种的险别决定能选的险种,投连，万能和传统险不能一起投保
 * @param contNo
 * @param insuredNo
 * @return
 */
function getMainRiskForTypeExclude(contNo, insuredNo) {
	var riskSet = [];
	var reLMRiskAppDB = new dB('LMRiskApp');
	reLMRiskAppDB['subriskflag'] = 'M';
	var reLMRiskAppSet = reLMRiskAppDB.query();
	if (reLMRiskAppSet != null && reLMRiskAppSet.length > 0) {
		for (var i = 0; i < reLMRiskAppSet.length; i++) {
			riskSet.push(reLMRiskAppSet[i]['riskcode']);
		}
	}
	return riskSet;
}

exports.getMainRiskForTypeExclude = getMainRiskForTypeExclude;

/**
 * 销售筛选
 * @param tGlobalInput
 * @param contNo
 * @param insuredNo
 * @return
 */
function getSaleRiskAndPlan(tGlobalInput, contNo, insuredNo) {
	var reRiskPlanAndRiskSaleConfDB = new dB('RISKPLANANDRISKSALECONF');
	var reRiskPlanAndRiskSaleConfSet = reRiskPlanAndRiskSaleConfDB.query();
	var reLSAppntDB = new dB('LSAppnt');
	reLSAppntDB['contno'] = contNo;
	reLSAppntDB.getInfo();

	var reLSInsuredDB = new dB('LSInsured');
	reLSInsuredDB['contno'] = contNo;
	reLSInsuredDB['insuredno'] = insuredNo;
	reLSInsuredDB.getInfo();

	// 出生日期
	var appntBirthday = reLSAppntDB['appntbirthday'];
	var insuredBirthday = reLSInsuredDB['birthday'];

	// 年龄
	var appntAge = reLSAppntDB['appntage'];
	var insuredAge = reLSInsuredDB['age'];

	//当前日期
	var currentDate = pubFun.getCurrentDate();
	if (appntBirthday != null && appntBirthday.length > 0) {
		appntAge = pubFun.calInterval(appntBirthday, currentDate, "Y");
	}
	if (insuredBirthday != null && insuredBirthday.length > 0) {
		insuredAge = pubFun.calInterval(insuredBirthday, currentDate, "Y");
	}

	// 渠道
	var chanle = tGlobalInput['chanlesign'];
	var managecom = tGlobalInput['ManageCom'];
	var set1 = [];
	var set2 = [];
	var set3 = [];
	// 排除的险种

	for (var i = 0; reRiskPlanAndRiskSaleConfSet != null && i < reRiskPlanAndRiskSaleConfSet.length; i++) {
		var reRiskPlanAndRiskSaleConfSchema = reRiskPlanAndRiskSaleConfSet[i];
		// 渠道
		if ('Chanle' == reRiskPlanAndRiskSaleConfSchema['type'] && chanle == reRiskPlanAndRiskSaleConfSchema['value1']) {
			set1.push(reRiskPlanAndRiskSaleConfSchema['riskandriskplancode']);
		}
		// 地区
		if ("ManageComGrpCode" == reRiskPlanAndRiskSaleConfSchema['type']) {
			set2.push(reRiskPlanAndRiskSaleConfSchema['riskandriskplancode']);
		}
		// 开始销售日期
		if ("StartDate" == reRiskPlanAndRiskSaleConfSchema['type']) {
			if (currentDate == pubFun.getLaterDate(currentDate, reRiskPlanAndRiskSaleConfSchema['value1'])) {
				set3.push(reRiskPlanAndRiskSaleConfSchema['riskandriskplancode']);
			}
		}
		// 结束销售日期
		if ("EndDate" == reRiskPlanAndRiskSaleConfSchema['type']) {
			if (currentDate == pubFun.getBeforeDate(currentDate, reRiskPlanAndRiskSaleConfSchema['value1'])) {
				set3.push(reRiskPlanAndRiskSaleConfSchema['riskandriskplancode']);
			}
		}

		// 年龄
		if ("AppAgeMax" == reRiskPlanAndRiskSaleConfSchema['type']) {
			var value1 = reRiskPlanAndRiskSaleConfSchema['value1'];
			var value2 = reRiskPlanAndRiskSaleConfSchema['value2'];
			if (value2 != null && value2 != '' && value2 == "Y") {
				if (appntAge > value1) {
					set3.push(reRiskPlanAndRiskSaleConfSchema['riskandriskplancode']);
				}
			} else if (appntBirthday != null && appntBirthday.length > 0) {
				var age = pubFun.calInterval(appntBirthday, currentDate, value2);
				if (age > value1) {
					set3.push(reRiskPlanAndRiskSaleConfSchema['riskandriskplancode']);
				}
			}
		}

		if ("AppAgeMin" == reRiskPlanAndRiskSaleConfSchema['type']) {
			var value1 = reRiskPlanAndRiskSaleConfSchema['value1'];
			var value2 = reRiskPlanAndRiskSaleConfSchema['value2'];
			if (value2 != null && value2 != '' && value2 == "Y") {
				if (appntAge < value1) {
					set3.push(reRiskPlanAndRiskSaleConfSchema['riskandriskplancode']);
				}
			} else if (appntBirthday != null && appntBirthday.length > 0) {
				var age = pubFun.calInterval(appntBirthday, currentDate, value2);
				if (age < value1) {
					set3.push(reRiskPlanAndRiskSaleConfSchema['riskandriskplancode']);
				}
			}
		}
		if ("AppAgeMin" == reRiskPlanAndRiskSaleConfSchema['type']) {
			var value1 = reRiskPlanAndRiskSaleConfSchema['value1'];
			var value2 = reRiskPlanAndRiskSaleConfSchema['value2'];
			if (value2 != null && value2 != '' && value2 == "Y") {
				if (appntAge < value1) {
					set3.push(reRiskPlanAndRiskSaleConfSchema['riskandriskplancode']);
				}
			} else if (appntBirthday != null && appntBirthday.length > 0) {
				var age = pubFun.calInterval(appntBirthday, currentDate, value2);
				if (age < value1) {
					set3.push(reRiskPlanAndRiskSaleConfSchema['riskandriskplancode']);
				}
			}
		}
		if ("InsuAgeMax" == reRiskPlanAndRiskSaleConfSchema['type']) {
			var value1 = reRiskPlanAndRiskSaleConfSchema['value1'];
			var value2 = reRiskPlanAndRiskSaleConfSchema['value2'];
			if (value2 != null && value2 != '' && value2 == "Y") {
				if (insuredAge > value1) {
					set3.push(reRiskPlanAndRiskSaleConfSchema['riskandriskplancode']);
				}
			} else if (insuredBirthday != null && insuredBirthday.length > 0) {
				var age = calInterval(insuredBirthday, currentDate, value2);
				if (age > value1) {
					set3.push(reRiskPlanAndRiskSaleConfSchema['riskandriskplancode']);
				}
			}
		}

		if ("InsuAgeMin" == reRiskPlanAndRiskSaleConfSchema['type']) {
			var value1 = reRiskPlanAndRiskSaleConfSchema['value1'];
			var value2 = reRiskPlanAndRiskSaleConfSchema['value2'];
			if (value2 != null && value2 != '' && value2 == "Y") {
				if (insuredAge < value1) {
					set3.push(reRiskPlanAndRiskSaleConfSchema['riskandriskplancode']);
				}
			} else if (insuredBirthday != null && insuredBirthday.length > 0) {
				var age = pubFun.calInterval(insuredBirthday, currentDate, value2);
				if (age < value1) {
					set3.push(reRiskPlanAndRiskSaleConfSchema['riskandriskplancode']);
				}
			}
		}
		// 性别
		var appSex = reLSAppntDB['appntsex'];
		var insuSex = reLSInsuredDB['sex'];
		if ("AppSex" == reRiskPlanAndRiskSaleConfSchema['type']) {
			var value1 = reRiskPlanAndRiskSaleConfSchema['value1'];
			if (appSex != value1) {
				set3.push(reRiskPlanAndRiskSaleConfSchema['riskandriskplancode']);
			}
		}
		if ("InsuSex" == reRiskPlanAndRiskSaleConfSchema['type']) {
			var value1 = reRiskPlanAndRiskSaleConfSchema['value1'];
			if (insuSex != value1) {
				set3.push(reRiskPlanAndRiskSaleConfSchema['riskandriskplancode']);
			}
		}

	}

	var Set = [];
	for (var i = 0; i < set1.length; i++) {
		var temp = set1[i];
		if (set2.indexOf(temp) != -1 && set3.indexOf(temp) == -1) {
			Set.push(temp);
		}
	}
	return Set;
}

exports.getSaleRiskAndPlan = getSaleRiskAndPlan;
/**
 * 根据险种类别获取险种
 * @param riskType
 * @return
 */
function getRiskForRiskType(riskType) {
	var set = [];
	var reLMRiskAppDB = new dB('LMRiskApp');
	if (riskType != null && riskType != '' && riskType.length > 0) {
		reLMRiskAppDB['riskprop'] = riskType;
	}
	var reLMRiskAppSet = reLMRiskAppDB.query();
	if (reLMRiskAppSet != null && reLMRiskAppSet.length > 0) {
		for (var i = 0; i < reLMRiskAppSet.length; i++) {
			var riskcode = reLMRiskAppSet[i]['riskcode'];
			set.push(riskcode);
		}
	}
	// 保险计划
	if (riskType == null || riskType == '' || riskType == 'PLAN') {
		var sql = "select distinct PlanCode from RiskPlan";
		var ssrs = exeSQL.execSQL(sql);
		if (ssrs != null && ssrs.length > 0) {
			for (var i = 0; i < ssrs.length; i++) {
				set.push(ssrs[i][0]);
			}
		}
	}
	return set;
}

exports.getRiskForRiskType = getRiskForRiskType;

/**
 * 根据用户销售权限组，筛选险种
 * @param tGlobalInput
 * @return
 */
function getRiskForRiskSaleGroup(tGlobalInput) {
	var set = [];
	var reLMRiskSaleGroupToRiskDB = new dB('LMRiskSaleGroupToRisk');
	if ("000000" == tGlobalInput['riskSaleGroup']) {
		// 全局数据
		var reLMRiskAppDB = new dB('LMRiskApp');
		var reLMRiskAppSet = reLMRiskAppDB.query();
		if (reLMRiskAppSet != null && reLMRiskAppSet.length > 0) {
			for (var i = 0; i < reLMRiskAppSet.length; i++) {
				var riskcode = reLMRiskAppSet[i]['riskcode'];
				set.push(riskcode);
			}
		}
		// 家庭计划
		var sql = "select distinct PlanCode from RiskPlan";
		var ssrs = exeSQL.execSQL(sql);
		if (ssrs != null && ssrs.length > 0) {
			for (var i = 0; i < ssrs.length; i++) {
				set.push(ssrs[i][0]);
			}
		}
	} else {
		reLMRiskSaleGroupToRiskDB['groupcode'] = tGlobalInput['riskSaleGroup'];
		var teLMRiskSaleGroupToRiskSet = reLMRiskSaleGroupToRiskDB.query();
		for (var i = 0; teLMRiskSaleGroupToRiskSet != null && i < teLMRiskSaleGroupToRiskSet.length; i++) {
			// 加入险种代码
			set.push(teLMRiskSaleGroupToRiskSet[i]['riskcode']);
		}
	}
	return set;

}

exports.getRiskForRiskSaleGroup = getRiskForRiskSaleGroup;

/**
 * 根据已有的险种查询可以投保的主险种
 * @param contNo
 * @param insuredNo
 * @return
 */
function getMainRiskForExist(contNo, insuredNo) {
	var set = [];
	var sql = "select * from LMRiskApp where SubRiskFlag='M' and riskcode not in (select riskcode from lspol where contno='" + contNo + "' and insuredno='" + insuredNo + "' and SubFlag='M')";
	// and poltypeflag = 'S' and remark is null
	var reLMRiskAppDB = new dB('LMRiskApp');
	var reLMRiskAppSet = reLMRiskAppDB.executeQuery(sql);
	for (var i = 0; i < reLMRiskAppSet.length; i++) {
		var riskcode = reLMRiskAppSet[i]['riskcode'];
		set.push(riskcode);
	}
	return set;
}

exports.getMainRiskForExist = getMainRiskForExist;
/**
 * 获取当前建议书被保人状态下可能的所有附加险,以后要对附加险作特殊处理可以在此加上（比如，趸缴主险不能附加附加险等）
 * @param tGlobalInput
 * @param contNo
 * @param insuredNo
 * @return
 */
function getSubRiskForCurrentCondition(tGlobalInput, contNo, insuredNo) {
	var set = [];
	// 查询出来当前所有一般险种保单
	var reLSPolDB = new dB('LSPol');
	reLSPolDB['contno'] = contNo;
	reLSPolDB['InsuredNo'] = insuredNo;
	reLSPolDB['poltypeflag'] = "S";
	var reLSPolSet = reLSPolDB.query();

	if (reLSPolSet == null || reLSPolSet.length == 0) {
		// set = getAllPossibleSubRisk(tGlobalInput, contNo, insuredNo,
		// "S");
	} else {
		for (var i = 0; reLSPolSet != null && i < reLSPolSet.length; i++) {
			var tempLSPolSchema = reLSPolSet[i];
			var riskcode = tempLSPolSchema['riskcode'];
			var polno = tempLSPolSchema['polno'];

			// 获取已经投保的附加险
			var tempSet = [];
			for (var j = 0; j < reLSPolSet.length; j++) {
				var tempSubLSPolSchema = reLSPolSet[j];
				var subriskcode = tempSubLSPolSchema['riskcode'];
				var mainpolno = tempSubLSPolSchema['mainpolno'];
				var subpolno = tempSubLSPolSchema['polno'];
				var subflag = tempSubLSPolSchema['subflag'];
				// 此处可以加特殊处理，直接跳过等，
				// .....
				if (mainpolno != subpolno && polno == mainpolno) {
					tempSet.push(subriskcode);
				}
				if (subflag == "S" && polno == mainpolno) {
					tempSet.push(subriskcode);
				}
			}
			// 通过查询关联表，获取可能的附加险
			var reLMRiskRelaDB = new dB('LMRiskRela');
			reLMRiskRelaDB['riskcode'] = riskcode;
			reLMRiskRelaDB['relacode'] = "01";
			// 一般主附关系
			reLMRiskRelaDB['salechnl'] = tGlobalInput['chanlesign'];
			var teLMRiskRelaSet = reLMRiskRelaDB.query();
			for (var k = 0; teLMRiskRelaSet != null && k < teLMRiskRelaSet.length; k++) {
				var releriskcode = teLMRiskRelaSet[k]['relariskcode'];
				if (tempSet.indexOf(releriskcode) == -1) {
					set.push(releriskcode);
				}
			}
		}
	}
	return set;
}

exports.getSubRiskForCurrentCondition = getSubRiskForCurrentCondition;

/**
 * 该主险下的所有可附加附加险和家庭险种
 * @param riskCode 主险代码
 * @param flag 家庭险标记 -1排除普通附加险，0排除家庭险
 * */
function getSubRiskForMainRisk(riskCode, flag) {
	var set = []
	var sql = "select riskcode from riskplan where plancode = '" + riskCode + "' and upnodeid = '" + riskCode + "' and bak1<>'" + flag + "'";
	var ssrs = exeSQL.execSQL(sql);
	for (var i = 0; ssrs != null && i < ssrs.length; i++) {
		set.push(ssrs[i][0]);
	}
	return set;
}

exports.getSubRiskForMainRisk = getSubRiskForMainRisk;
